﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using System.Windows.Forms.Design;
using DevExpress.XtraEditors;

namespace ProgNetComponentsDx.Shortcut
{
    public partial class FormShortcutEdit : XtraForm
    {
        protected bool CloseByKey = false;

        private KeyShortcut _ShortcutValue;
        public KeyShortcut ShortcutValue
        {
            get
            {
                return _ShortcutValue;
            }
            set
            {
                _ShortcutValue = value;
                SetText();
            }
        }
        public Keys Keys
        {
            get { return ShortcutValue.Key; }
            set { ShortcutValue = new KeyShortcut(value); }
        }
        public FormShortcutEdit(Keys value)
        {
            InitializeComponent();
            
            if (_ShortcutValue == null)
                _ShortcutValue = new KeyShortcut(value);
            SetText();
        }
        public FormShortcutEdit()
            : this(Keys.None)
        {

        }

        public static Keys ShowShortcutEditor(IWindowsFormsEditorService edSvc, Keys value)
        {
            FormShortcutEdit form = new FormShortcutEdit(value);
            DialogResult dlg = DialogResult.None;
            if (edSvc != null)
                dlg = edSvc.ShowDialog(form);
            else dlg = form.ShowDialog();

            if (dlg == DialogResult.OK)
                return form.ShortcutValue.Key;
            else
                return value;
        }

        private void SetText()
        {
            if (_ShortcutValue.IsExist)
                txtEditor.Text = _ShortcutValue.ToString();
            else
                txtEditor.Text = "";
            txtEditor.SelectionStart = txtEditor.Text.Length;
        }

        private bool IsFitKey(KeyEventArgs e)
        {
            if (e.KeyCode == (Keys.LButton | Keys.ShiftKey) || e.KeyCode == Keys.ShiftKey || (e.KeyCode == (Keys.RButton | Keys.ShiftKey) || e.KeyCode == (Keys.MButton | Keys.ShiftKey)))
                return false;
            else if (e.KeyData == Keys.Back)
            {
                ShortcutValue = new KeyShortcut(ShortcutValue.Key);
                SetText();
                return false;
            }
            else if (e.KeyData == (Keys.LButton | Keys.MButton | Keys.Back))
            {
                DialogResult = DialogResult.OK;
                return false;
            }
            else if (e.KeyData == (Keys.LButton | Keys.RButton | Keys.Back | Keys.ShiftKey))
            {
                DialogResult = DialogResult.Cancel;
                return false;
            }
            else if (e.KeyCode >= (Keys.ShiftKey | Keys.Space) && (e.KeyCode <= (Keys)90 && !e.Alt) && !e.Control)
                return false;
            else
                return true;
        }

        private void txtEditor_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            if (IsFitKey(e))
            {
                ShortcutValue = new KeyShortcut(e.KeyData);
                SetText();
            }
        }

        private void txtEditor_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtEditor_TextChanged(object sender, EventArgs e)
        {
            SetText();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            _ShortcutValue = new KeyShortcut();
            SetText();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseByKey = true;
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            CloseByKey = true;
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (!CloseByKey)
                e.Cancel = true;
            base.OnFormClosing(e);
        }
    }
}
